/* charnico.cpp
* Character Nico: class
*/
#include "charnico.h"

//------------------------------------------------------------------------------
//- Class : CharacterNico
//------------------------------------------------------------------------------
/* Constructor 1
*
*/
CharacterNico::CharacterNico() : Character("data/CharacterNico.cfg") {
  /* Bounding Box */
  __setBoundingBox(160, 0, 210, 200);
  __enableBoundingBox(1);
  __lockBoundingBox(1);
  _spriteBuffer = create_bitmap(370, 354);
  _spriteAlpha = load_bitmap("images/charnico/alpha.bmp", NULL );
  __linkStatus(3, 4, 1);           // Immunity
  __linkStatus(2, ANIMATION_3, 2); // Create the hitbox
  __linkStatus(4, ANIMATION_3, 4);
}

/* Destructor
*
*/
CharacterNico::~CharacterNico() {
  int i, j;
  for(i = 0; i < MAX_ANIMATION; ++i)
    for(j = 0; j < MAX_FRAME_PER_ANIMATION; ++j)
      if ( _bmpArr[i][j] ) destroy_bitmap(_bmpArr[i][j]);
}
/* __readKeyboard() : main function to read any keyboard event
*
*/
void CharacterNico::__readKeyboard() {
  if (_status&STATUS_GUARD) return;
  if ( key[KEY_RIGHT]) {
    __moveX(4);
    _view = VIEW_RIGHT;
  }
  if ( key[KEY_LEFT] ) {
    __moveX(-4);
    _view = VIEW_LEFT;
    if (__getX() + 160 < 0 ) __setX(-160);
  }
  if ( key[KEY_G] ) {
    __changeSequence(ANIMATION_5);
    _status |= STATUS_GUARD;
    _bbx2 = 232;
  }
  if ( key[KEY_SPACE] ) {
    __changeSequence(ANIMATION_2);
  }
  if ( key[KEY_LCONTROL] && !(_status&STATUS_ATTACKING) ) {
    __changeSequence(ANIMATION_3);
    _status |= STATUS_ATTACKING;   
  }  
}

/* __update() : update the character and its animation
*
*/
void CharacterNico::__update() {
  Entity * hit;
  // Character update
  int status = __updateAnimation();
  switch(status) {
    case 2: // Attacking
      hit = new eHit(BoundingBox(0,0,115,50), 5, 6);
      hit->__setX(__getX() + 215);
      if ( _view == VIEW_LEFT ) hit->__moveX(-170);

      hit->__setY(50);
      _entVec->push_back(hit);
    break;
    case 4: // Guarding
      _status |= STATUS_ATTACKING;
      _status ^= STATUS_ATTACKING;
      status = 1;
    break;    
    case 3: // Guarding
      _status |= STATUS_GUARD;
      _status ^= STATUS_GUARD;
      status = 1;
      _bbx2 = 210;
    break;
  }
  if ( status == 1 ) __changeSequence(ANIMATION_1);
}
/* __shuriken()
*
*/
void CharacterNico::__shuriken() { }


/*  __getItem()
*
*/
void CharacterNico::__getItem(int id, int value1, int value2, int value3) { }

/* __linkHUD()
*
*/
void CharacterNico::__linkHUD(vector<HUD *>* HUDVec) {
  HUDVec->push_back(_HUDLife);
}

/* __getSprite()
*
*/
BITMAP * CharacterNico::__getSprite() {
  //<dev>
  clear_bitmap(_spriteBuffer);
  blit(_spriteAlpha, _spriteBuffer, 0, 0, 0, 0, 370, 354);
  if ( _view == VIEW_LEFT )
    draw_sprite_h_flip(_spriteBuffer, _sprite, 0, 0);
  else
    draw_sprite(_spriteBuffer, _sprite, 0, 0);
    
  return _spriteBuffer;
  // </dev>  
}
